package com.google.code.jholidays.base;

import com.google.code.jholidays.core.DescriptorCollection;
import com.google.code.jholidays.core.EventDescriptor;

public class TestHelper {

    public DescriptorCollection generateFixedCollection() {
	DescriptorCollection result = new DescriptorCollection();

	/*
	 * 1-0 2-1 3-1 4-3 5-3 8-4 9-4 10-5 7-5 14-8 15-8 6-10 11-10 12-6 13-6
	 */

	int nodesCount = 15;

	int[][] table = new int[][] { { 1, 0 }, { 2, 1 }, { 3, 1 }, { 4, 3 },
		{ 5, 3 }, { 8, 4 }, { 9, 4 }, { 10, 5 }, { 7, 5 }, { 14, 8 },
		{ 15, 8 }, { 6, 10 }, { 11, 10 }, { 12, 6 }, { 13, 6 } };

	for (int i = 0; i < nodesCount; ++i) {
	    int itemID = table[i][0];
	    int parentID = table[i][1];

	    EventDescriptor item = new EventDescriptor(itemID, parentID);
	    result.add(item);

	}

	return result;
    }

    public DescriptorCollection generateCollection(int[][] table)
	    throws IllegalArgumentException {
	DescriptorCollection result = new DescriptorCollection();

	if (table != null && table.length > 0) {
	    checkDimmensions(table);

	    for (int i = 0; i < table.length; ++i) {
		int itemID = table[i][0];
		int parentID = table[i][1];

		EventDescriptor item = new EventDescriptor(itemID, parentID);
		result.add(item);
	    }
	}

	return result;
    }

    private void checkDimmensions(int[][] table)
	    throws IllegalArgumentException {
	if (table[0].length != 2)
	    throw new IllegalArgumentException(
		    "Table must have two columns. Get "
			    + Integer.toString(table[0].length) + " columns");
    }
}
